package cn.njxzc.book.masterexamples.ch13;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import sun.swing.table.DefaultTableCellHeaderRenderer;
public class StudentInfoManagement extends JFrame implements ActionListener{
 /**
	 * serialVersionUID:TODO（用一句话描述这个变量表示什么）
	 * @since Ver 1.1
	 */
	
	private static final long serialVersionUID = 1L;
JLabel jl=new JLabel("注册成功!",JLabel.LEFT);
 JLabel jl1=new JLabel("编号:");
 JLabel jl2=new JLabel("姓名:");
 JLabel jl3=new JLabel("院系:");
 JTextField jtf1=new JTextField(5);
 JTextField jtf2=new JTextField(8);
 String[] college={"计算机学院","理工学院","管理学院"};
 JComboBox<?> jcb=new JComboBox(college);
 JButton jb1=new JButton("注册");
 JButton jb2=new JButton("注销");
 JButton jb3=new JButton("查询");
 JButton jb4=new JButton("更新");
 Object[][]data=null;
 String[]columnName={"学号","姓名","院系"};
 DefaultTableModel dtm=new DefaultTableModel(data,columnName);
 JTable jt=new JTable(dtm);
 JScrollPane jsp=new JScrollPane(jt);
 JPanel jp=new JPanel();
 JPanel jp1=new JPanel();
 JPanel jp11=new JPanel();
 JPanel jp12=new JPanel();
 JPanel jp13=new JPanel();
 JPanel jp2=new JPanel();
 StudentInfoManagement(){
  super("smsGUI");
  jb1.addActionListener(this);
  jb2.addActionListener(this);
  jb3.addActionListener(this);
  jb4.addActionListener(this);
  jp11.setLayout(new FlowLayout(FlowLayout.LEFT));
  jp11.add(jl1);jp11.add(jtf1);jp11.add(jl2);jp11.add(jtf2);
  jp12.setLayout(new FlowLayout(FlowLayout.LEFT));
  jp12.add(jl3);jp12.add(jcb);
  jp13.add(jb1);jp13.add(jb2);jp13.add(jb3);jp13.add(jb4);
  jp1.setLayout(new GridLayout(3,1));
  jp1.add(jp11);jp1.add(jp12);jp1.add(jp13);
  jp1.setBorder(BorderFactory.createEtchedBorder());
  jp2.setLayout(new BorderLayout());
  // 设置table表头居左
     DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();
     thr.setHorizontalAlignment(JLabel.LEFT);
     jt.getTableHeader().setDefaultRenderer(thr);
     DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
      public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
       if (row % 2 == 0)
        setBackground(Color.white); //设置奇数行底色
       else if (row % 2 == 1)
        setBackground(new Color(246, 246, 246)); //设置偶数行底色
       return super.getTableCellRendererComponent(table, value,
         isSelected, hasFocus, row, column);
          }
        };
     // 设置table内容居左
  tcr.setHorizontalAlignment(JLabel.LEFT);
  jt.setDefaultRenderer(Object.class, tcr);
     jp2.add(jsp);
  jp.setLayout(null);
  jp1.setBounds(2,0, 380, 130);
  jp2.setBounds(5,140, 375, 200);
  jl.setBounds(0, 340, 400, 20);
  jp.add(jp1);jp.add(jp2);jp.add(jl);
   
  add(jp);
  setSize(400,400);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent ae) {
  if(ae.getSource()==jb1){
   String[]row=new String[3];
   row[0]=jtf1.getText().trim();
   row[1]=jtf2.getText().trim();
   row[2]=(String)jcb.getSelectedItem();
   dtm.addRow(row);
  }
 }
 public static void main(String[] args) {
  new StudentInfoManagement();
 }
}